home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S2.ZIP / src / libgplus.5 / libibert / strsigna.c < prev    next >
C/C++ Source or Header  |  1993-09-02  |  15KB  |  635 lines

  1. /* Extended support for using signal values.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.    Written by Fred Fish.  fnf@cygnus.com
  4.  
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10.  
  11. Libiberty is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with libiberty; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include <ansidecl.h>
  22.  
  23. #include "config.h"
  24.  
  25. #ifdef LOSING_SYS_SIGLIST
  26. #define sys_siglist no_such_symbol
  27. #endif
  28.  
  29. #include <stdio.h>
  30. #include <signal.h>
  31.  
  32. /*  Routines imported from standard C runtime libraries. */
  33.  
  34. #ifdef __STDC__
  35. #include <stddef.h>
  36. extern void *malloc (size_t size);                /* 4.10.3.3 */
  37. extern void *memset (void *s, int c, size_t n);            /* 4.11.6.1 */
  38. #else    /* !__STDC__ */
  39. #ifndef const
  40. #define const
  41. #endif
  42. extern char *malloc ();        /* Standard memory allocater */
  43. extern char *memset ();
  44. #endif    /* __STDC__ */
  45.  
  46. #ifdef LOSING_SYS_SIGLIST
  47. #undef sys_siglist
  48. #endif
  49.  
  50.  
  51. #ifndef NULL
  52. #  ifdef __STDC__
  53. #    define NULL (void *) 0
  54. #  else
  55. #    define NULL 0
  56. #  endif
  57. #endif
  58.  
  59. #ifndef MAX
  60. #  define MAX(a,b) ((a) > (b) ? (a) : (b))
  61. #endif
  62.  
  63. /* Translation table for signal values.
  64.  
  65.    Note that this table is generally only accessed when it is used at runtime
  66.    to initialize signal name and message tables that are indexed by signal
  67.    value.
  68.  
  69.    Not all of these signals will exist on all systems.  This table is the only
  70.    thing that should have to be updated as new signal numbers are introduced.
  71.    It's sort of ugly, but at least its portable. */
  72.  
  73. struct signal_info
  74. {
  75.   int value;        /* The numeric value from <signal.h> */
  76.   char *name;        /* The equivalent symbolic value */
  77.   char *msg;        /* Short message about this value */
  78. };
  79.  
  80. static const struct signal_info signal_table[] =
  81. {
  82. #if defined (SIGHUP)
  83.   {SIGHUP, "SIGHUP", "Hangup"},
  84. #endif
  85. #if defined (SIGINT)
  86.   {SIGINT, "SIGINT", "Interrupt"},
  87. #endif
  88. #if defined (SIGQUIT)
  89.   {SIGQUIT, "SIGQUIT", "Quit"},
  90. #endif
  91. #if defined (SIGILL)
  92.   {SIGILL, "SIGILL", "Illegal instruction"},
  93. #endif
  94. #if defined (SIGTRAP)
  95.   {SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"},
  96. #endif
  97. /* Put SIGIOT before SIGABRT, so that if SIGIOT==SIGABRT then SIGABRT
  98.    overrides SIGIOT.  SIGABRT is in ANSI and POSIX.1, and SIGIOT isn't. */
  99. #if defined (SIGIOT)
  100.   {SIGIOT, "SIGIOT", "IOT trap"},
  101. #endif
  102. #if defined (SIGABRT)
  103.   {SIGABRT, "SIGABRT", "Aborted"},
  104. #endif
  105. #if defined (SIGEMT)
  106.   {SIGEMT, "SIGEMT", "Emulation trap"},
  107. #endif
  108. #if defined (SIGFPE)
  109.   {SIGFPE, "SIGFPE", "Arithmetic exception"},
  110. #endif
  111. #if defined (SIGKILL)
  112.   {SIGKILL, "SIGKILL", "Killed"},
  113. #endif
  114. #if defined (SIGBUS)
  115.   {SIGBUS, "SIGBUS", "Bus error"},
  116. #endif
  117. #if defined (SIGSEGV)
  118.   {SIGSEGV, "SIGSEGV", "Segmentation fault"},
  119. #endif
  120. #if defined (SIGSYS)
  121.   {SIGSYS, "SIGSYS", "Bad system call"},
  122. #endif
  123. #if defined (SIGPIPE)
  124.   {SIGPIPE, "SIGPIPE", "Broken pipe"},
  125. #endif
  126. #if defined (SIGALRM)
  127.   {SIGALRM, "SIGALRM", "Alarm clock"},
  128. #endif
  129. #if defined (SIGTERM)
  130.   {SIGTERM, "SIGTERM", "Terminated"},
  131. #endif
  132. #if defined (SIGUSR1)
  133.   {SIGUSR1, "SIGUSR1", "User defined signal 1"},
  134. #endif
  135. #if defined (SIGUSR2)
  136.   {SIGUSR2, "SIGUSR2", "User defined signal 2"},
  137. #endif
  138. /* Put SIGCLD before SIGCHLD, so that if SIGCLD==SIGCHLD then SIGCHLD
  139.    overrides SIGCLD.  SIGCHLD is in POXIX.1 */
  140. #if defined (SIGCLD)
  141.   {SIGCLD, "SIGCLD", "Child status changed"},
  142. #endif
  143. #if defined (SIGCHLD)
  144.   {SIGCHLD, "SIGCHLD", "Child status changed"},
  145. #endif
  146. #if defined (SIGPWR)
  147.   {SIGPWR, "SIGPWR", "Power fail/restart"},
  148. #endif
  149. #if defined (SIGWINCH)
  150.   {SIGWINCH, "SIGWINCH", "Window size changed"},
  151. #endif
  152. #if defined (SIGURG)
  153.   {SIGURG, "SIGURG", "Urgent I/O condition"},
  154. #endif
  155. #if defined (SIGIO)
  156.   /* "I/O pending" has also been suggested, but is misleading since the
  157.      signal only happens when the process has asked for it, not everytime
  158.      I/O is pending. */
  159.   {SIGIO, "SIGIO", "I/O possible"},
  160. #endif
  161. #if defined (SIGPOLL)
  162.   {SIGPOLL, "SIGPOLL", "Pollable event occurred"},
  163. #endif
  164. #if defined (SIGSTOP)
  165.   {SIGSTOP, "SIGSTOP", "Stopped (signal)"},
  166. #endif
  167. #if defined (SIGTSTP)
  168.   {SIGTSTP, "SIGTSTP", "Stopped (user)"},
  169. #endif
  170. #if defined (SIGCONT)
  171.   {SIGCONT, "SIGCONT", "Continued"},
  172. #endif
  173. #if defined (SIGTTIN)
  174.   {SIGTTIN, "SIGTTIN", "Stopped (tty input)"},
  175. #endif
  176. #if defined (SIGTTOU)
  177.   {SIGTTOU, "SIGTTOU", "Stopped (tty output)"},
  178. #endif
  179. #if defined (SIGVTALRM)
  180.   {SIGVTALRM, "SIGVTALRM", "Virtual timer expired"},
  181. #endif
  182. #if defined (SIGPROF)
  183.   {SIGPROF, "SIGPROF", "Profiling timer expired"},
  184. #endif
  185. #if defined (SIGXCPU)
  186.   {SIGXCPU, "SIGXCPU", "CPU time limit exceeded"},
  187. #endif
  188. #if defined (SIGXFSZ)
  189.   {SIGXFSZ, "SIGXFSZ", "File size limit exceeded"},
  190. #endif
  191. #if defined (SIGWIND)
  192.   {SIGWIND, "SIGWIND", "SIGWIND"},
  193. #endif
  194. #if defined (SIGPHONE)
  195.   {SIGPHONE, "SIGPHONE", "SIGPHONE"},
  196. #endif
  197. #if defined (SIGLOST)
  198.   {SIGLOST, "SIGLOST", "Resource lost"},
  199. #endif
  200. #if defined (SIGWAITING)
  201.   {SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"},
  202. #endif
  203. #if defined (SIGLWP)
  204.   {SIGLWP, "SIGLWP", "Signal LWP"},
  205. #endif
  206. #if defined (SIGDANGER)
  207.   {SIGDANGER, "SIGDANGER", "Swap space dangerously low"},
  208. #endif
  209. #if defined (SIGGRANT)
  210.   {SIGGRANT, "SIGGRANT", "Monitor mode granted"},
  211. #endif
  212. #if defined (SIGRETRACT)
  213.   {SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"},
  214. #endif
  215. #if defined (SIGMSG)
  216.   {SIGMSG, "SIGMSG", "Monitor mode data available"},
  217. #endif
  218. #if defined (SIGSOUND)
  219.   {SIGSOUND, "SIGSOUND", "Sound completed"},
  220. #endif
  221. #if defined (SIGSAK)
  222.   {SIGSAK, "SIGSAK", "Secure attention"},
  223. #endif
  224.   {0, NULL, NULL}
  225. };
  226.  
  227. /* Translation table allocated and initialized at runtime.  Indexed by the
  228.    signal value to find the equivalent symbolic value. */
  229.  
  230. static char **signal_names;
  231. static int num_signal_names = 0;
  232.  
  233. /* Translation table allocated and initialized at runtime, if it does not
  234.    already exist in the host environment.  Indexed by the signal value to find
  235.    the descriptive string.
  236.  
  237.    We don't export it for use in other modules because even though it has the
  238.    same name, it differs from other implementations in that it is dynamically
  239.    initialized rather than statically initialized. */
  240.  
  241. #ifdef NEED_sys_siglist
  242.  
  243. static int sys_nsig;
  244. static char **sys_siglist;
  245.  
  246. #else
  247.  
  248. static int sys_nsig = NSIG;
  249. extern const char * const sys_siglist[];
  250.  
  251. #endif
  252.  
  253.  
  254. /*
  255.  
  256. NAME
  257.  
  258.     init_signal_tables -- initialize the name and message tables
  259.  
  260. SYNOPSIS
  261.  
  262.     static void init_signal_tables ();
  263.  
  264. DESCRIPTION
  265.  
  266.     Using the signal_table, which is initialized at compile time, generate
  267.     the signal_names and the sys_siglist (if needed) tables, which are
  268.     indexed at runtime by a specific signal value.
  269.  
  270. BUGS
  271.  
  272.     The initialization of the tables may fail under low memory conditions,
  273.     in which case we don't do anything particularly useful, but we don't
  274.     bomb either.  Who knows, it might succeed at a later point if we free
  275.     some memory in the meantime.  In any case, the other routines know
  276.     how to deal with lack of a table after trying to initialize it.  This
  277.     may or may not be considered to be a bug, that we don't specifically
  278.     warn about this particular failure mode.
  279.  
  280. */
  281.  
  282. static void
  283. init_signal_tables ()
  284. {
  285.   const struct signal_info *eip;
  286.   int nbytes;
  287.  
  288.   /* If we haven't already scanned the signal_table once to find the maximum
  289.      signal value, then go find it now. */
  290.  
  291.   if (num_signal_names == 0)
  292.     {
  293.       for (eip = signal_table; eip -> name != NULL; eip++)
  294.     {
  295.       if (eip -> value >= num_signal_names)
  296.         {
  297.           num_signal_names = eip -> value + 1;
  298.         }
  299.     }
  300.     }
  301.  
  302.   /* Now attempt to allocate the signal_names table, zero it out, and then
  303.      initialize it from the statically initialized signal_table. */
  304.  
  305.   if (signal_names == NULL)
  306.     {
  307.       nbytes = num_signal_names * sizeof (char *);
  308.       if ((signal_names = (char **) malloc (nbytes)) != NULL)
  309.     {
  310.       memset (signal_names, 0, nbytes);
  311.       for (eip = signal_table; eip -> name != NULL; eip++)
  312.         {
  313.           signal_names[eip -> value] = eip -> name;
  314.         }
  315.     }
  316.     }
  317.  
  318. #ifdef NEED_sys_siglist
  319.  
  320.   /* Now attempt to allocate the sys_siglist table, zero it out, and then
  321.      initialize it from the statically initialized signal_table. */
  322.  
  323.   if (sys_siglist == NULL)
  324.     {
  325.       nbytes = num_signal_names * sizeof (char *);
  326.       if ((sys_siglist = (char **) malloc (nbytes)) != NULL)
  327.     {
  328.       memset (sys_siglist, 0, nbytes);
  329.       sys_nsig = num_signal_names;
  330.       for (eip = signal_table; eip -> name != NULL; eip++)
  331.         {
  332.           sys_siglist[eip -> value] = eip -> msg;
  333.         }
  334.     }
  335.     }
  336.  
  337. #endif
  338.  
  339. }
  340.  
  341.  
  342. /*
  343.  
  344. NAME
  345.  
  346.     signo_max -- return the max signo value
  347.  
  348. SYNOPSIS
  349.  
  350.     int signo_max ();
  351.  
  352. DESCRIPTION
  353.  
  354.     Returns the maximum signo value for which a corresponding symbolic
  355.     name or message is available.  Note that in the case where
  356.     we use the sys_siglist supplied by the system, it is possible for
  357.     there to be more symbolic names than messages, or vice versa.
  358.     In fact, the manual page for psignal(3b) explicitly warns that one
  359.     should check the size of the table (NSIG) before indexing it,
  360.     since new signal codes may be added to the system before they are
  361.     added to the table.  Thus NSIG might be smaller than value
  362.     implied by the largest signo value defined in <signal.h>.
  363.  
  364.     We return the maximum value that can be used to obtain a meaningful
  365.     symbolic name or message.
  366.  
  367. */
  368.  
  369. int
  370. signo_max ()
  371. {
  372.   int maxsize;
  373.  
  374.   if (signal_names == NULL)
  375.     {
  376.       init_signal_tables ();
  377.     }
  378.   maxsize = MAX (sys_nsig, num_signal_names);
  379.   return (maxsize - 1);
  380. }
  381.  
  382.  
  383. /*
  384.  
  385. NAME
  386.  
  387.     strsignal -- map a signal number to a signal message string
  388.  
  389. SYNOPSIS
  390.  
  391.     char *strsignal (int signo)
  392.  
  393. DESCRIPTION
  394.  
  395.     Maps an signal number to an signal message string, the contents of
  396.     which are implementation defined.  On systems which have the external
  397.     variable sys_siglist, these strings will be the same as the ones used
  398.     by psignal().
  399.  
  400.     If the supplied signal number is within the valid range of indices
  401.     for the sys_siglist, but no message is available for the particular
  402.     signal number, then returns the string "Signal NUM", where NUM is the
  403.     signal number.
  404.  
  405.     If the supplied signal number is not a valid index into sys_siglist,
  406.     returns NULL.
  407.  
  408.     The returned string is only guaranteed to be valid only until the
  409.     next call to strsignal.
  410.  
  411. */
  412.  
  413. char *
  414. strsignal (signo)
  415.   int signo;
  416. {
  417.   char *msg;
  418.   static char buf[32];
  419.  
  420. #ifdef NEED_sys_siglist
  421.  
  422.   if (signal_names == NULL)
  423.     {
  424.       init_signal_tables ();
  425.     }
  426.  
  427. #endif
  428.  
  429.   if ((signo < 0) || (signo >= sys_nsig))
  430.     {
  431.       /* Out of range, just return NULL */
  432.       msg = NULL;
  433.     }
  434.   else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
  435.     {
  436.       /* In range, but no sys_siglist or no entry at this index. */
  437.       sprintf (buf, "Signal %d", signo);
  438.       msg = buf;
  439.     }
  440.   else
  441.     {
  442.       /* In range, and a valid message.  Just return the message. */
  443.       msg = (char*)sys_siglist[signo];
  444.     }
  445.   
  446.   return (msg);
  447. }
  448.  
  449.  
  450. /*
  451.  
  452. NAME
  453.  
  454.     strsigno -- map an signal number to a symbolic name string
  455.  
  456. SYNOPSIS
  457.  
  458.     char *strsigno (int signo)
  459.  
  460. DESCRIPTION
  461.  
  462.     Given an signal number, returns a pointer to a string containing
  463.     the symbolic name of that signal number, as found in <signal.h>.
  464.  
  465.     If the supplied signal number is within the valid range of indices
  466.     for symbolic names, but no name is available for the particular
  467.     signal number, then returns the string "Signal NUM", where NUM is
  468.     the signal number.
  469.  
  470.     If the supplied signal number is not within the range of valid
  471.     indices, then returns NULL.
  472.  
  473. BUGS
  474.  
  475.     The contents of the location pointed to are only guaranteed to be
  476.     valid until the next call to strsigno.
  477.  
  478. */
  479.  
  480. char *
  481. strsigno (signo)
  482.   int signo;
  483. {
  484.   char *name;
  485.   static char buf[32];
  486.  
  487.   if (signal_names == NULL)
  488.     {
  489.       init_signal_tables ();
  490.     }
  491.  
  492.   if ((signo < 0) || (signo >= num_signal_names))
  493.     {
  494.       /* Out of range, just return NULL */
  495.       name = NULL;
  496.     }
  497.   else if ((signal_names == NULL) || (signal_names[signo] == NULL))
  498.     {
  499.       /* In range, but no signal_names or no entry at this index. */
  500.       sprintf (buf, "Signal %d", signo);
  501.       name = buf;
  502.     }
  503.   else
  504.     {
  505.       /* In range, and a valid name.  Just return the name. */
  506.       name = signal_names[signo];
  507.     }
  508.  
  509.   return (name);
  510. }
  511.  
  512.  
  513. /*
  514.  
  515. NAME
  516.  
  517.     strtosigno -- map a symbolic signal name to a numeric value
  518.  
  519. SYNOPSIS
  520.  
  521.     int strtosigno (char *name)
  522.  
  523. DESCRIPTION
  524.  
  525.     Given the symbolic name of a signal, map it to a signal number.
  526.     If no translation is found, returns 0.
  527.  
  528. */
  529.  
  530. int
  531. strtosigno (name)
  532.   char *name;
  533. {
  534.   int signo = 0;
  535.  
  536.   if (name != NULL)
  537.     {
  538.       if (signal_names == NULL)
  539.     {
  540.       init_signal_tables ();
  541.     }
  542.       for (signo = 0; signo < num_signal_names; signo++)
  543.     {
  544.       if ((signal_names[signo] != NULL) &&
  545.           (strcmp (name, signal_names[signo]) == 0))
  546.         {
  547.           break;
  548.         }
  549.     }
  550.       if (signo == num_signal_names)
  551.     {
  552.       signo = 0;
  553.     }
  554.     }
  555.   return (signo);
  556. }
  557.  
  558.  
  559. /*
  560.  
  561. NAME
  562.  
  563.     psignal -- print message about signal to stderr
  564.  
  565. SYNOPSIS
  566.  
  567.     void psignal (unsigned signo, char *message);
  568.  
  569. DESCRIPTION
  570.  
  571.     Print to the standard error the message, followed by a colon,
  572.     followed by the description of the signal specified by signo,
  573.     followed by a newline.
  574. */
  575.  
  576. #ifdef NEED_psignal
  577.  
  578. void
  579. psignal (signo, message)
  580.   unsigned signo;
  581.   char *message;
  582. {
  583.   if (signal_names == NULL)
  584.     {
  585.       init_signal_tables ();
  586.     }
  587.   if ((signo <= 0) || (signo >= sys_nsig))
  588.     {
  589.       fprintf (stderr, "%s: unknown signal\n", message);
  590.     }
  591.   else
  592.     {
  593.       fprintf (stderr, "%s: %s\n", message, sys_siglist[signo]);
  594.     }
  595. }
  596.  
  597. #endif    /* NEED_psignal */
  598.  
  599.  
  600. /* A simple little main that does nothing but print all the signal translations
  601.    if MAIN is defined and this file is compiled and linked. */
  602.  
  603. #ifdef MAIN
  604.  
  605. main ()
  606. {
  607.   int signo;
  608.   int maxsigno;
  609.   char *name;
  610.   char *msg;
  611.   char *strsigno ();
  612.   char *strsignal ();
  613.  
  614.   maxsigno = signo_max ();
  615.   printf ("%d entries in names table.\n", num_signal_names);
  616.   printf ("%d entries in messages table.\n", sys_nsig);
  617.   printf ("%d is max useful index.\n", maxsigno);
  618.  
  619.   /* Keep printing values until we get to the end of *both* tables, not
  620.      *either* table.  Note that knowing the maximum useful index does *not*
  621.      relieve us of the responsibility of testing the return pointer for
  622.      NULL. */
  623.  
  624.   for (signo = 0; signo <= maxsigno; signo++)
  625.     {
  626.       name = strsigno (signo);
  627.       name = (name == NULL) ? "<NULL>" : name;
  628.       msg = strsignal (signo);
  629.       msg = (msg == NULL) ? "<NULL>" : msg;
  630.       printf ("%-4d%-18s%s\n", signo, name, msg);
  631.     }
  632. }
  633.  
  634. #endif
  635.